package fun.yizhierha.operation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import fun.yizhierha.common.base.BaseErrDto;
import fun.yizhierha.common.utils.PageUtils;
import fun.yizhierha.common.utils.Query;
import fun.yizhierha.common.utils.SecurityUtils;
import fun.yizhierha.common.utils.ValidList;
import fun.yizhierha.common.utils.file.ExcelUtils;
import fun.yizhierha.operation.domain.OraServer;
import fun.yizhierha.operation.domain.vo.CreateOraServerVo;
import fun.yizhierha.operation.domain.vo.UpdateOraServerVo;
import fun.yizhierha.operation.domain.vo.RetrieveOraServerVo;
import fun.yizhierha.operation.mapper.OraServerMapper;
import fun.yizhierha.operation.service.mapstruct.OraServerMapstruct;
import fun.yizhierha.operation.service.OraServerService;
import fun.yizhierha.operation.util.ExecuteShellUtil;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

import java.io.Closeable;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

/** generated by EH-Admin
* @author xaopohi
* @date Wed Dec 21 13:25:19 CST 2022
**/
@Service
@RequiredArgsConstructor
public class OraServerServiceImpl extends ServiceImpl<OraServerMapper, OraServer> implements OraServerService{

    private final OraServerMapstruct oraServerMapstruct;

    @Override
    public PageUtils<OraServer> list(RetrieveOraServerVo retrieveOraServerVo, Query.PageVo pageVo) {
        QueryWrapper<OraServer> wrapper = new QueryWrapper<>();
        String name = retrieveOraServerVo.getName();

        if (name != null){
            wrapper.like(OraServer.COL_NAME,name);
        }

        IPage<OraServer> iPage = baseMapper.selectPage(new Query<OraServer>().getPage(pageVo), wrapper);
        return new PageUtils<>(iPage);
    }

    @Override
    public synchronized void save(CreateOraServerVo createOraServerVo) {
        // 1.字段为UNI，需要不重复
        UserDetails currentUser = SecurityUtils.getCurrentUser();
        createOraServerVo.setCreateBy(currentUser.getUsername());
        // 2.映射数据
        OraServer oraServer = oraServerMapstruct.toOraServer(createOraServerVo);
        oraServer.setCreateTime(new Timestamp(new Date().getTime()));
        // 3.保存    
        this.save(oraServer);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public synchronized void edit(ValidList<UpdateOraServerVo> updateOraServerVoList, List<BaseErrDto> errDtoList) {
        List<OraServer> toUpdateOraServerList = new ArrayList<>();

        for (UpdateOraServerVo updateOraServerVo : updateOraServerVoList) {
            Long id = updateOraServerVo.getId();
            // 1.字段为UNI，需要不重复
            UserDetails currentUser = SecurityUtils.getCurrentUser();
            updateOraServerVo.setUpdateBy(currentUser.getUsername());
            OraServer oraServer = oraServerMapstruct.toOraServer(updateOraServerVo);
            oraServer.setUpdateTime(new Timestamp(new Date().getTime()));

            toUpdateOraServerList.add(oraServer);
        }

        // 2.更新
        this.updateBatchById(toUpdateOraServerList);
    }

    @Override
    public void remove(Set<Long> ids) {
        this.removeByIds(ids);
    }

    @Override
    public void download(HttpServletResponse response) {
        ExcelUtils.export(response,"服务器信息表",this.list(), OraServer.class);
    }

    @Override
    public OraServer findByIp(String ip) {
        OraServer byIp = getBaseMapper().findByIp(ip);
        return byIp;
    }

    @Override
    public Boolean testConnect(CreateOraServerVo vo) {
        ExecuteShellUtil shellUtil = null;
        try{
            shellUtil = new ExecuteShellUtil(vo.getIp(), vo.getAccount(), vo.getPassword(), vo.getPort());
            return shellUtil.execute("ls") == 0;
        }catch (Exception e){
            return false;
        }finally {
            if (shellUtil != null) shellUtil.close();
        }
    }

}